//
//  MainWebController.m
//  insurance
//
//  Created by li on 2018/6/19.
//  Copyright © 2018年 li. All rights reserved.
//

#import "MainWebController.h"
#import "QuotePrintController.h"
#import "CheckPrintController.h"
#import "MeetcarPrintController.h"
#import "QRCodeViewController.h"

@interface MainWebController ()

@end

@implementation MainWebController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    NSString *url = [NSString stringWithFormat:@"%@?version=%@",[AppConfig mainWebURL],[AppConfig webVersion] ?: @"v0.0.1"];
    [self setUrlString:url];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newUserLogin) name:KNewUserLoginSuceessNotification object:nil];
    self.navigationItem.leftBarButtonItem = nil;
//    [self jsGetqrCode];
}

- (void)newUserLogin{
    NSString *url = [NSString stringWithFormat:@"%@",[AppConfig mainWebURL]];
    [self setUrlString:url];
}

- (void)jsbridge{
    [super jsbridge];
    [self printQuote];
    [self printOrder];
    [self printCheckCar];
    [self printMeetCar];
    [self jsGetqrCode];
}

- (void)printQuote{
    [self.bridge registerHandler:kJSPrintQuote handler:^(id data, WVJBResponseCallback responseCallback) {
        NSDictionary *dic = data;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data options:NSJSONWritingPrettyPrinted error:nil];
        NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        QuotePrintData *model = [[QuotePrintData alloc] initWithDictionary:dic];
        QuotePrintController *con = [[QuotePrintController alloc] init];
        con.typeName = @"报价单";
        con.data = model;
        [self.navigationController pushViewController:con animated:YES];
        HSLog(@"这是全部数据%@",jsonStr);
    }];
}

- (void)printOrder{
    [self.bridge registerHandler:kJSPrintOder handler:^(id data, WVJBResponseCallback responseCallback) {
        NSDictionary *dic = data;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data options:NSJSONWritingPrettyPrinted error:nil];
        NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        QuotePrintData *model = [[QuotePrintData alloc] initWithDictionary:dic];
        QuotePrintController *con = [[QuotePrintController alloc] init];
        con.typeName = @"结算单";
        
        con.data = model;
        [self.navigationController pushViewController:con animated:YES];
        HSLog(@"这是全部数据%@",jsonStr);
    }];
}

- (void)printCheckCar{
    [self.bridge registerHandler:kJSprintCheckCar handler:^(id data, WVJBResponseCallback responseCallback) {
        NSDictionary *dic = data;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data options:NSJSONWritingPrettyPrinted error:nil];
        NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        CheckPrintData *model = [[CheckPrintData alloc] initWithDictionary:dic];
        HSLog(@"这是全部数据%@",jsonStr);
        CheckPrintController *con = [[CheckPrintController alloc] init];
        con.data = model;
        [self.navigationController pushViewController:con animated:YES];
    }];
}

- (void)printMeetCar{
    [self.bridge registerHandler:kJSPrintMeetCar handler:^(id data, WVJBResponseCallback responseCallback) {
        NSDictionary *dic = data;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data options:NSJSONWritingPrettyPrinted error:nil];
        NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        MeetcarPrintData *model = [[MeetcarPrintData alloc] initWithDictionary:dic];
        MeetcarPrintController *con = [[MeetcarPrintController alloc] init];
        con.data = model;
        [self.navigationController pushViewController:con animated:YES];
        HSLog(@"这是全部数据%@",jsonStr);
    }];
}

- (void)jsGetqrCode{
    [self.bridge registerHandler:kJSGetQrCode handler:^(id data, WVJBResponseCallback responseCallback) {
        
        QRCodeViewController *con = [[QRCodeViewController alloc] init];
        con.title = @"扫一扫";
        con.qrcodeContent = ^(NSString *result) {
            if (result.length) {
                NSString *resultString = [NSString stringWithFormat:@"{content:\"%@\"}",result];
                responseCallback(resultString);
            }
            HSLog(@"二维码内容:%@",result);
        };
        [self.navigationController pushViewController:con animated:YES];
    }];
 
}

//- (void)jsPrintInsuranceInfo{
//    [self.bridge registerHandler:kJSPrintInsurance handler:^(id data, WVJBResponseCallback responseCallback) {
//        NSDictionary *dic = data;
//
//        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data options:NSJSONWritingPrettyPrinted error:nil];
//        NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
//        NSLog(@"这是全部数据%@",jsonStr);
//
//        InsuredDataModel *model =  [[InsuredDataModel alloc] initWithDictionary:dic];
//        InsurancePrintController *con = [[InsurancePrintController alloc] init];
//        con.printData = model;
//        [self.navigationController pushViewController:con animated:YES];
//    }];
//}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:KNewUserLoginSuceessNotification object:nil];
}

@end
